import 'package:flutter/services.dart';
import 'log_utils.dart';

///获取通道进行蓝牙管理
///初始化 BluetoothChannel.init();
class BluetoothChannel {
  ///通道标识
  static String bluetoothChannelName = "samples.flutter.io/bluetooth";

  ///Flutter与Android之间互相传递数据的通道
  static BasicMessageChannel? _bluetoothChannel;

  ///BluetoothAdapter.STATE_OFF
  ///蓝牙已关闭
  static const int STATE_OFF = 10;

  ///BluetoothAdapter.STATE_TURNING_ON
  ///蓝牙正在开启
  static const int STATE_TURNING_ON = 11;

  ///BluetoothAdapter.STATE_ON
  ///蓝牙已开启
  static const int STATE_ON = 12;

  ///BluetoothAdapter.STATE_TURNING_OFF
  ///蓝牙正在关闭
  static const int STATE_TURNING_OFF = 13;

  ///是否已经与设备连接
  static bool isConnectedDevice = false;

  ///是否已开启蓝牙
  static bool isOpenBluetooth = false;

  ///初始化通道
  static init() {
    if (_bluetoothChannel == null) {
      _bluetoothChannel =
          BasicMessageChannel(bluetoothChannelName, StandardMessageCodec());
    }

    ///接收Android端发来的消息
    ///
    // _bluetoothChannel.setMessageHandler((message) async {
    //   MyLog.i("message:$message");
    //   if (message is Map) {
    //     bool connectState = message["connectState"];
    //     Fluttertoast.showToast(msg: connectState ? "连接成功" : "连接失败");
    //   }
    //   return "ok";
    // });
  }

  static BasicMessageChannel? get bluetoothChannel {
    if (_bluetoothChannel == null) {
      _bluetoothChannel =
          BasicMessageChannel(bluetoothChannelName, StandardMessageCodec());
    }
    return _bluetoothChannel;
  }

  ///获取蓝牙状态
  ///true开启  false关闭
  static Future<bool> getBluetoothState() async {
    var state = await _bluetoothChannel!.send("getBluetoothState");
    MyLog.i("getBluetoothState: $state");
    switch (state) {
      case STATE_ON:
      case STATE_TURNING_ON:
        isOpenBluetooth = true;
        return true;
      case STATE_OFF:
      case STATE_TURNING_OFF:
        isOpenBluetooth = false;
        return false;
      default:
        return false;
    }
  }

  ///开启或关闭蓝牙
  ///[isOpen] true开启
  static bool openOrCloseBluetooth(bool isOpen) {
    if (isOpen) {
      MyLog.w("开启蓝牙");
      bluetoothChannel!.send("openBluetooth");
      return true;
    } else {
      MyLog.w("关闭蓝牙");
      bluetoothChannel!.send("closeBluetooth");
      isConnectedDevice = false;
      return false;
    }
  }

  ///获取本机蓝牙名称
  static Future<String> getLocalDeviceName() async {
    String deviceName = await bluetoothChannel!.send("getLocalDeviceName");
    return deviceName;
  }

  ///获取已绑定的蓝牙设备
  ///返回[list<Map<String,String>>]
  ///eg.[{"name":"OnePlus","address":"A:b:c:D:e"},{"name":"ZC 328","address":"1:2:3:4:5"}]
  static Future<List> getBondedDevices() async {
    List list = await bluetoothChannel!.send("getBondedDevices");
    // for (int i = 0; i < list.length; i++) {
    //   MyLog.i(list[i]["name"]);
    //   MyLog.i(list[i]["address"]);
    // }
    return list;
  }

  ///[map]{"address":蓝牙设备Mac地址,"name":设备名称}
  ///
  ///根据Mac地址连接蓝牙
  static Future connectDevice(Map map) async {
    bluetoothChannel!.send({"method": "connectDevice", "data": map});
  }
}
